package com.ruoyi.web.controller.system;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.system.domain.bo.CommitInfo;
import com.ruoyi.system.domain.bo.FileBo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.FirmwareVo;
import com.ruoyi.system.domain.bo.FirmwareBo;
import com.ruoyi.system.service.IFirmwareService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 固件管理
 *
 * @author whc
 * @date 2024-05-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/firmware")
public class FirmwareController extends BaseController {

    private final IFirmwareService iFirmwareService;

    @SaCheckPermission("system:firmware:gitInfo")
    @GetMapping("/gitInfo")
    public List<CommitInfo> getFileCommits(String filePath) {
       return iFirmwareService.getFileCommits(filePath);
    }

    /**
     * 查询固件管理列表
     */
    @SaCheckPermission("system:firmware:list")
    @GetMapping("/list")
    public TableDataInfo<FirmwareVo> list(FirmwareBo bo, PageQuery pageQuery) {
        return iFirmwareService.queryPageList(bo, pageQuery);
    }
    /**
     * 存储文件
     */
    @SaCheckPermission("system:firmware:uploadFirmware")
    @PostMapping("/uploadFirmware")
    public R<Void> uploadFirmware(@ModelAttribute  FileBo fileBo) throws Exception {
        return toAjax(iFirmwareService.uploadFirmware(fileBo));
    }
    /**
     * 获取文件
     */
    @SaCheckPermission("system:firmware:getFile")
    @GetMapping("/getFile")
    public ResponseEntity<FileSystemResource> getFile(String path, String commitId) throws Exception {
        if (commitId == null) {
            return iFirmwareService.getFile(path);
        }
        return iFirmwareService.getFile(path, commitId);
    }

    /**
     * 导出固件管理列表
     */
    @SaCheckPermission("system:firmware:export")
    @Log(title = "固件管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(FirmwareBo bo, HttpServletResponse response) {
        List<FirmwareVo> list = iFirmwareService.queryList(bo);
        ExcelUtil.exportExcel(list, "固件管理", FirmwareVo.class, response);
    }

    /**
     * 获取固件管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:firmware:query")
    @GetMapping("/{id}")
    public R<FirmwareVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iFirmwareService.queryById(id));
    }

    /**
     * 新增固件管理
     */
    @SaCheckPermission("system:firmware:add")
    @Log(title = "固件管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody FirmwareBo bo) {
        return toAjax(iFirmwareService.insertByBo(bo));
    }

    /**
     * 修改固件管理
     */
    @SaCheckPermission("system:firmware:edit")
    @Log(title = "固件管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody FirmwareBo bo) {
        return toAjax(iFirmwareService.updateByBo(bo));
    }

    /**
     * 删除固件管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:firmware:remove")
    @Log(title = "固件管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iFirmwareService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
